﻿using Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;

namespace BLL
{
    public class Cliente_BLL
    {
        #region singleton
        private static Cliente_BLL instance;

        private Cliente_BLL() { }

        public static Cliente_BLL Instance
        {
            get
            {
                if (instance == null)
                {
                    instance = new Cliente_BLL();
                }
                return instance;
            }
        }
        #endregion

        public List<hrp_empresas> ObterEmpresasDoCliente(int id_cliente, int userid)
        {
            hrpEntities hrp = new hrpEntities();
            //var list = hrp.hrp_empresas.Where(e => e.id_cliente == id_cliente && e.activo == true);
            var list = from empresa_user in hrp.hrp_empresas
                       join user_valid in hrp.hrp_users_empresas on empresa_user.id equals user_valid.id_empresa
                       where user_valid.id_user == userid && user_valid.activo == true
                       select empresa_user;
            return list.ToList();
        }

        public List<hrp_empresas> ObterEmpresasDoClienteTotal(int id_cliente)
        {
            hrpEntities hrp = new hrpEntities();
            var list = hrp.hrp_empresas.Where(e => e.id_cliente == id_cliente);
      
            return list.ToList();
        }

        public hrp_clientes ObterCliente(string email)
        {
            hrpEntities hrp = new hrpEntities();
            var clientes = from cliente in hrp.hrp_clientes
                           where cliente.email == email && cliente.activo == true
                           select cliente;

            return clientes.FirstOrDefault();
        }

        //Todo: Usar a tabela users
        public hrp_clientes ObterClientePorID(int id)
        {
            hrpEntities hrp = new hrpEntities();
            var clientes = from cliente in hrp.hrp_clientes
                           join user in hrp.hrp_users on cliente.id equals user.idClient
                           where user.id == id
                           //join member in hrp.cmsmember on cliente.email equals member.EMAIL
                           //where member.NODEID == id
                           select cliente;

            return clientes.FirstOrDefault();

        }

        //Todo: criar classe Settings
        public hrp_temp CriaCliente(string email, string token, string ip)
        {
            try
            {
                hrpEntities hrp = new hrpEntities();
                hrp_temp temp = hrp.hrp_temp.FirstOrDefault(n => n.email == email && n.token == token && n.activo == false);
                temp.activo = true;
                hrp_clientes cliente = new hrp_clientes();
                cliente.activo = true;
                cliente.email = temp.email;
                cliente.nome_cliente = temp.nome;
                cliente.user_id = "";//Definicoes.UtilizadorGenerico;
                cliente.user_ip = temp.user_ip;
                cliente.user_timestamp = DateTime.Now;
                cliente.data_registo = temp.user_timestamp;
                hrp.hrp_clientes.Add(cliente);
                hrp.SaveChanges();


                hrp_users newUser = new hrp_users();
                newUser.username = email;
                newUser.password = temp.password;
                newUser.createDate = DateTime.Now;

                var idClient = from clientes in hrp.hrp_clientes
                               where clientes.email == email
                               select cliente.id;

                newUser.idClient = Convert.ToInt32(idClient);

                return temp;
            }
            catch (Exception) { }
            return null;
        }

        public bool ClienteValido(string email, string token)
        {
            hrpEntities hrp = new hrpEntities();
            return hrp.hrp_temp.Any(n => n.email == email && n.token == token && n.activo == false);
        }

        public void inserirUserTemp(string email, string nome, string password, string token, string ip)
        {
            hrpEntities hrp = new hrpEntities();
            hrp_temp novoTemp = new hrp_temp();
            novoTemp.activo = false;
            novoTemp.email = email;
            novoTemp.nome = nome;
            novoTemp.password = password;
            novoTemp.token = token;
            novoTemp.user_id = "";// Definicoes.UtilizadorGenerico;
            novoTemp.user_ip = ip;
            novoTemp.user_timestamp = DateTime.Now;
            hrp.hrp_temp.Add(novoTemp);
            hrp.SaveChanges();

        }

        public hrp_users loginClient(string username, string password)
        {
            hrpEntities hrp = new hrpEntities();
            var user = from users in hrp.hrp_users
                       join clientes in hrp.hrp_clientes on users.idClient equals clientes.id                       
                       where users.username == username && users.password == password
                       select users;
            return user.FirstOrDefault();
        }

        public hrp_produtos_clientes checkValid(int idclient)
        {
            hrpEntities hrp = new hrpEntities();
            var valid = hrp.hrp_produtos_clientes.FirstOrDefault(c=> c.idcliente==idclient && c.activo == true && DateTime.Now <= c.data_fim);
            return valid;       
                         
        }

        public void updatePassword(int id, string password)
        {
            hrpEntities hrp = new hrpEntities();
            hrp_users user = new hrp_users();
            var currentUser = hrp.hrp_users.FirstOrDefault(c => c.id == id);
            currentUser.password = password;
            hrp.SaveChanges();
        }

        public bool validUserEmail(string email)
        {

            hrpEntities hrp = new hrpEntities();
            hrp_users user = new hrp_users();
            var currentUser = hrp.hrp_users.FirstOrDefault(c => c.username == email);
            if (currentUser != null)
            {
                return true;
            }
            return false;
        }

        public string newPassword(string email, string newPassword)
        {
            hrpEntities hrp = new hrpEntities();
            hrp_users user = new hrp_users();
            var currentUser = hrp.hrp_users.FirstOrDefault(c => c.username == email);
            currentUser.password = newPassword;
            hrp.SaveChanges();
            return newPassword;
        }

        public List<hrp_users> listUsers(int idcliente)
        {
            hrpEntities hrp = new hrpEntities();
            var users = from user in hrp.hrp_users
                        where user.idClient == idcliente
                        select user;

            return users.ToList();

        }

        public bool activateDeactivateUser(int iduser)
        {
            hrpEntities hrp = new hrpEntities();
            var users = hrp.hrp_users.FirstOrDefault(c => c.id == iduser);
            if (users.active == true)
            {
                users.active = false;
                hrp.SaveChanges();
                return false;

            }
            else { users.active = true; hrp.SaveChanges(); }

            return true;
        }

        public hrp_users ObterUserporID(int iduser)
        {
            hrpEntities hrp = new hrpEntities();
            var users = from user in hrp.hrp_users
                        where user.id == iduser
                        select user;

            return users.FirstOrDefault();
        }

        public bool novoUtilizador(string nome, string email, int idclient, string password)
        {
           
            //check if email exists.
            hrpEntities hrp = new hrpEntities();
            hrp_users users = new hrp_users();

            var user_mail = hrp.hrp_users.FirstOrDefault(c => c.username == email);

            if (user_mail != null)
            {
               
                return false;
            }

            try
            {
                users.active = true;
                users.createDate = DateTime.Now;
                users.idClient = idclient;
                users.Name = nome;
                users.username = email;
                //Guid g;
                //g = Guid.NewGuid();
                //string newPassword = g.ToString();
                //newPassword = newPassword.Substring(0, 7);
                users.password = password ;
                hrp.hrp_users.Add(users);
                hrp.SaveChanges();
                
            }
            catch (Exception)
            {
                return false;
                throw;
            }
            return true;

        }

        public hrp_produtos obterProdutoCliente(int idclient)
        {
            hrpEntities hrp = new hrpEntities();
            hrp_produtos_clientes hrp_prod = new hrp_produtos_clientes();
            var produto_cliente = from prod in hrp.hrp_produtos
                                  join tipo_prod in hrp.hrp_produtos_clientes on prod.id equals tipo_prod.idproduto
                                  where tipo_prod.idcliente == idclient
                                  select prod;

            return produto_cliente.FirstOrDefault();
            //return hrp.hrp_produtos_clientes.FirstOrDefault(c => c.activo == true && c.idcliente == idclient);
        }


    }
}
